//-----------------------------------------------------------------------
// <copyright file="a_Articles_ImgDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_Articles_ImgDAOBase
    {
        #region Common methods
        public virtual a_Articles_Img Createa_Articles_ImgFromReader(IDataReader reader)
        {
            a_Articles_Img item = new a_Articles_Img();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("IID"))) item.IID = (int)reader["IID"];
                if (!reader.IsDBNull(reader.GetOrdinal("AID"))) item.AID = (int)reader["AID"];
                if (!reader.IsDBNull(reader.GetOrdinal("ImageNo"))) item.ImageNo = (int)reader["ImageNo"];
                if (!reader.IsDBNull(reader.GetOrdinal("ImageW"))) item.ImageW = (int)reader["ImageW"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("CountryID"))) item.CountryID = (int)reader["CountryID"];
                if (!reader.IsDBNull(reader.GetOrdinal("FYear"))) item.FYear = (int)reader["FYear"];
                if (!reader.IsDBNull(reader.GetOrdinal("SeasonID"))) item.SeasonID = (int)reader["SeasonID"];
                if (!reader.IsDBNull(reader.GetOrdinal("WTypeID"))) item.WTypeID = (string)reader["WTypeID"];
                if (!reader.IsDBNull(reader.GetOrdinal("SexID"))) item.SexID = (int)reader["SexID"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsMainImg"))) item.IsMainImg = (bool)reader["IsMainImg"];
                if (!reader.IsDBNull(reader.GetOrdinal("Keywords"))) item.Keywords = (string)reader["Keywords"];
                if (!reader.IsDBNull(reader.GetOrdinal("ImgOrientation"))) item.ImgOrientation = (bool)reader["ImgOrientation"];
                if (!reader.IsDBNull(reader.GetOrdinal("LayoutFormat"))) item.LayoutFormat = (byte)reader["LayoutFormat"];
                if (!reader.IsDBNull(reader.GetOrdinal("StatPopularity"))) item.StatPopularity = (int)reader["StatPopularity"];
                if (!reader.IsDBNull(reader.GetOrdinal("RatingCount"))) item.RatingCount = (int)reader["RatingCount"];
                if (!reader.IsDBNull(reader.GetOrdinal("RatingAvg"))) item.RatingAvg = (int)reader["RatingAvg"];
                if (!reader.IsDBNull(reader.GetOrdinal("ZoomCount"))) item.ZoomCount = (int)reader["ZoomCount"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Articles_Img methods
            
        public virtual void Createa_Articles_Img(a_Articles_Img a_Articles_Img)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Articles_Img_ins");
                
                database.AddInParameter(dbCommand, "@AID", DbType.Int32, a_Articles_Img.AID);
                database.AddInParameter(dbCommand, "@ImageNo", DbType.Int32, a_Articles_Img.ImageNo);
                database.AddInParameter(dbCommand, "@ImageW", DbType.Int32, a_Articles_Img.ImageW);
                database.AddInParameter(dbCommand, "@Title", DbType.String, a_Articles_Img.Title);
                database.AddInParameter(dbCommand, "@CountryID", DbType.Int32, a_Articles_Img.CountryID);
                database.AddInParameter(dbCommand, "@FYear", DbType.Int32, a_Articles_Img.FYear);
                database.AddInParameter(dbCommand, "@SeasonID", DbType.Int32, a_Articles_Img.SeasonID);
                database.AddInParameter(dbCommand, "@WTypeID", DbType.AnsiString, a_Articles_Img.WTypeID);
                database.AddInParameter(dbCommand, "@SexID", DbType.Int32, a_Articles_Img.SexID);
                database.AddInParameter(dbCommand, "@IsMainImg", DbType.Boolean, a_Articles_Img.IsMainImg);
                database.AddInParameter(dbCommand, "@Keywords", DbType.String, a_Articles_Img.Keywords);
                database.AddInParameter(dbCommand, "@ImgOrientation", DbType.Boolean, a_Articles_Img.ImgOrientation);
                database.AddInParameter(dbCommand, "@LayoutFormat", DbType.Byte, a_Articles_Img.LayoutFormat);
                database.AddInParameter(dbCommand, "@StatPopularity", DbType.Int32, a_Articles_Img.StatPopularity);
                database.AddInParameter(dbCommand, "@RatingCount", DbType.Int32, a_Articles_Img.RatingCount);
                database.AddInParameter(dbCommand, "@RatingAvg", DbType.Int32, a_Articles_Img.RatingAvg);
                database.AddInParameter(dbCommand, "@ZoomCount", DbType.Int32, a_Articles_Img.ZoomCount);
                database.AddOutParameter(dbCommand, "@IID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_Articles_Img.IID = (int)database.GetParameterValue(dbCommand, "@IID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Articles_Img methods
        
        public virtual void Updatea_Articles_Img(a_Articles_Img a_Articles_Img)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Articles_Img_upd");            
                
                database.AddInParameter(dbCommand, "@IID", DbType.Int32, a_Articles_Img.IID);
                database.AddInParameter(dbCommand, "@AID", DbType.Int32, a_Articles_Img.AID);
                database.AddInParameter(dbCommand, "@ImageNo", DbType.Int32, a_Articles_Img.ImageNo);
                database.AddInParameter(dbCommand, "@ImageW", DbType.Int32, a_Articles_Img.ImageW);
                database.AddInParameter(dbCommand, "@Title", DbType.String, a_Articles_Img.Title);
                database.AddInParameter(dbCommand, "@CountryID", DbType.Int32, a_Articles_Img.CountryID);
                database.AddInParameter(dbCommand, "@FYear", DbType.Int32, a_Articles_Img.FYear);
                database.AddInParameter(dbCommand, "@SeasonID", DbType.Int32, a_Articles_Img.SeasonID);
                database.AddInParameter(dbCommand, "@WTypeID", DbType.AnsiString, a_Articles_Img.WTypeID);
                database.AddInParameter(dbCommand, "@SexID", DbType.Int32, a_Articles_Img.SexID);
                database.AddInParameter(dbCommand, "@IsMainImg", DbType.Boolean, a_Articles_Img.IsMainImg);
                database.AddInParameter(dbCommand, "@Keywords", DbType.String, a_Articles_Img.Keywords);
                database.AddInParameter(dbCommand, "@ImgOrientation", DbType.Boolean, a_Articles_Img.ImgOrientation);
                database.AddInParameter(dbCommand, "@LayoutFormat", DbType.Byte, a_Articles_Img.LayoutFormat);
                database.AddInParameter(dbCommand, "@StatPopularity", DbType.Int32, a_Articles_Img.StatPopularity);
                database.AddInParameter(dbCommand, "@RatingCount", DbType.Int32, a_Articles_Img.RatingCount);
                database.AddInParameter(dbCommand, "@RatingAvg", DbType.Int32, a_Articles_Img.RatingAvg);
                database.AddInParameter(dbCommand, "@ZoomCount", DbType.Int32, a_Articles_Img.ZoomCount);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Articles_Img methods
        public virtual void Deletea_Articles_Img(int iid)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Articles_Img_del");
                
                database.AddInParameter(dbCommand, "@IID", DbType.Int32, iid);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_Articles_Img methods
        
        public virtual a_Articles_Img Geta_Articles_Img(int iid)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Articles_Img_sel");
                
                database.AddInParameter(dbCommand, "@IID", DbType.Int32, iid);
                
                a_Articles_Img a_Articles_Img =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Articles_Img = Createa_Articles_ImgFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Articles_Img;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_Articles_ImgList methods
        public virtual a_Articles_ImgCollection Geta_Articles_ImgList(a_Articles_ImgColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Articles_Img_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_Articles_ImgCollection a_Articles_ImgCollection = new a_Articles_ImgCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Articles_Img a_Articles_Img = Createa_Articles_ImgFromReader(reader);
                        a_Articles_ImgCollection.Add(a_Articles_Img);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_Articles_ImgCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_Articles_ImgCollection Geta_Articles_ImgList(a_Articles_ImgColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_Articles_ImgList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion

        #region Geta_Articles_ImgMainByAID methods

        public virtual a_Articles_Img Geta_Articles_ImgMainByAID(int aid)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Articles_Img_selImgMainByArticle");

                database.AddInParameter(dbCommand, "@ArtID", DbType.Int32, aid);

                a_Articles_Img a_Articles_Img = null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Articles_Img = Createa_Articles_ImgFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Articles_Img;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Geta_Articles_ImgListByAID methods
        public virtual a_Articles_ImgCollection Geta_Articles_ImgListByAID( int Aid, a_Articles_ImgColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Articles_Img_selListByAID");

                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddInParameter(dbCommand, "@AID", DbType.Int32, Aid);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);

                a_Articles_ImgCollection a_Articles_ImgCollection = new a_Articles_ImgCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Articles_Img a_Articles_Img = Createa_Articles_ImgFromReader(reader);
                        a_Articles_ImgCollection.Add(a_Articles_Img);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_Articles_ImgCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual a_Articles_ImgCollection Geta_Articles_ImgListByAID(int Aid, a_Articles_ImgColumns orderBy, OrderDirection orderDirection)
        {
            int totalRecords = 0;
            return Geta_Articles_ImgListByAID(Aid, orderBy, orderDirection, 0, 0, out totalRecords);
        }

        #endregion

        #region Geta_Articles_ImgListFav methods
        public virtual a_Articles_ImgCollection Geta_Articles_ImgListFav(int abId, a_Articles_ImgColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Articles_Img_selListFav");

                database.AddInParameter(dbCommand, "@idAlbum", DbType.Int32, abId);
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);

                a_Articles_ImgCollection a_Articles_ImgCollection = new a_Articles_ImgCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Articles_Img a_Articles_Img = Createa_Articles_ImgFromReader(reader);
                        a_Articles_ImgCollection.Add(a_Articles_Img);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_Articles_ImgCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual a_Articles_ImgCollection Geta_Articles_ImgListFav(int abId, a_Articles_ImgColumns orderBy, OrderDirection orderDirection)
        {
            int totalRecords = 0;
            return Geta_Articles_ImgListFav(abId, orderBy, orderDirection, 0, 0, out totalRecords);
        }

        #endregion

        //[a_articleFeaturedUpload]
        public void articleFeaturedUpload(
            string Action,
            int IID,
            int pCount1,
            string strParam1,
            string strParam2)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_articleFeaturedUpload");

                database.AddInParameter(dbCommand, "@IID", DbType.Int32, IID);
                database.AddInParameter(dbCommand, "@Action", DbType.String, Action);
                database.AddInParameter(dbCommand, "@pCount1", DbType.Int32, pCount1);
                database.AddInParameter(dbCommand, "@strParam1", DbType.String, strParam1);
                database.AddInParameter(dbCommand, "@strParam2", DbType.String, strParam2);
               
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    
    }
}